﻿namespace ufida.u9.ait.openapi
{
	using System;
	using System.Collections.Generic;
    using System.Text;
    using ufida.u9.ait.openapi.utils;
    using UFIDA.U9.Base;
    using UFIDA.U9.Base.Currency;
    using UFIDA.U9.CBO.FI.BankAccount;
    using UFIDA.U9.CBO.Pub.Controller;
    using UFIDA.U9.CBO.SCM.Customer;
    using UFIDA.U9.PM.PM_Util;
    using UFSoft.UBF.AopFrame;
    using UFSoft.UBF.Business;
    using UFSoft.UBF.Util.Context;

    /// <summary>
    /// CustomerBankSaveSV partial 
    /// </summary>	
	public partial class CustomerBankSaveSV 
	{	
		internal BaseStrategy Select()
		{
			return new CustomerBankSaveSVImpementStrategy();	
		}		
	}
	
	#region  implement strategy	
	/// <summary>
	/// Impement Implement
	/// 
	/// </summary>	
	internal partial class CustomerBankSaveSVImpementStrategy : BaseStrategy
	{
		public CustomerBankSaveSVImpementStrategy() { }

		public override object Do(object obj)
		{						
			CustomerBankSaveSV bpObj = (CustomerBankSaveSV)obj;
			try
			{
                if (bpObj.Data == null || bpObj.Data.Count == 0)
                    throw new Exception("传入参数为空");
                List<CommonArchiveDataDTOData> retlist = new List<CommonArchiveDataDTOData>();

				foreach (var dto in bpObj.Data)
				{
					//多组织情况下,需要按照组织+编码形式查找
					Customer be = Customer.Finder.Find($"Code='{dto.Code}' and Org='{Context.LoginOrg.ID}'");
					if (be == null)
					{
						//新增
						throw new Exception($"客户：[{be.Code}]查找不到数据");
					}
					else
					{
                        //修改
                        retlist.AddRange(UpdateCustomerBank(be, dto));
					}
				}
                return ResultUtils.success(bpObj, bpObj.Data, retlist);
            }
            catch (Exception ex)
			{
                return ResultUtils.error(bpObj, bpObj.Data, ex);
            }
		}

        private List<CommonArchiveDataDTOData> UpdateCustomerBank(Customer be, CustomAITDTOData dto)
        {
            List<CommonArchiveDataDTOData> retlist = new List<CommonArchiveDataDTOData>();

            using (ISession session = Session.Open())
			{
                //将现有银行放入hash表
                Dictionary<String, CustomerBankAccount> cache = new Dictionary<string, CustomerBankAccount>();
                foreach (var item in be.CustomerBankAccount)
                {
                    cache.Add(item.Code, item);
                }

                //增对DTO每行,新增或修改
                CustomerBankAccount bank = null;
                foreach (var bankdto in dto.CustomerBankAccount)
                {
                    if (cache.ContainsKey(bankdto.Code))
                    {
                        //修改客户银行账号
                        bank = UpdateCustomerBankAcc(cache[bankdto.Code], bankdto);
                    }
                    else
                    {
                        //新增客户银行账号
                        bank = AddCustomerBankAcc(be, bankdto);
                    }

                    //添加返回值
                    retlist.Add(new CommonArchiveDataDTOData
                    {
                        ID = bank.ID,
                        Name = bank.Name,
                        Code = bank.Code,
                    });
                }

                session.Commit();
			}

            return retlist;
        }

        private CustomerBankAccount AddCustomerBankAcc(Customer be, CustomBankAITDTOData bankdto)
        {
            //创建银行账号
            CustomerBankAccount account = CustomerBankAccount.Create(be);
            account.Code=bankdto.Code;
            account.Name=bankdto.Name;
            if (bankdto.Bank == null) throw new Exception($"Bank 为空");
            account.Bank = EntityUtils.GetEntityByCode<Bank>(bankdto.Bank.Code);
            //创建账号币种
            CustomerAccountCurrency line = CustomerAccountCurrency.Create(account);
            line.IsDefault=true;
            line.Currency = EntityUtils.GetEntityByCode<Currency>("C001");

            return account;
        }

        private CustomerBankAccount UpdateCustomerBankAcc(CustomerBankAccount account, CustomBankAITDTOData bankdto)
        {
            account.Name=bankdto.Name;
            if (bankdto.Bank == null) throw new Exception($"Bank 为空");
            account.Bank = EntityUtils.GetEntityByCode<Bank>(bankdto.Bank.Code);
            return account;
        }
    }

	#endregion
	
	
}